import 'package:flutter/material.dart';
import 'package:habit_master/core/models/habit.dart';
import 'package:intl/intl.dart';

class HabitCard extends StatelessWidget {
  final Habit habit;
  final VoidCallback onTap;
  final Function(bool) onToggle;
  
  const HabitCard({
    Key? key,
    required this.habit,
    required this.onTap,
    required this.onToggle,
  }) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    final bool isCompleted = habit.isCompletedToday();
    
    return InkWell(
      onTap: onTap,
      borderRadius: BorderRadius.circular(10),
      child: Card(
        elevation: 2,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10),
        ),
        child: Padding(
          padding: const EdgeInsets.all(15),
          child: Row(
            children: [
              _buildIconCircle(),
              const SizedBox(width: 15),
              _buildHabitInfo(),
              _buildCheckbox(isCompleted),
            ],
          ),
        ),
      ),
    );
  }
  
  Widget _buildIconCircle() {
    return Container(
      width: 45,
      height: 45,
      decoration: BoxDecoration(
        color: const Color(0xFFE3F2FD),
        borderRadius: BorderRadius.circular(25),
      ),
      child: Icon(
        habit.icon,
        color: const Color(0xFF4CAF50),
        size: 20,
      ),
    );
  }
  
  Widget _buildHabitInfo() {
    final timeFormat = DateFormat('HH:mm');
    final reminderTime = '${timeFormat.format(
      DateTime(
        2023, 1, 1, 
        habit.reminderTime.hour, 
        habit.reminderTime.minute
      )
    )}';
    
    return Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            habit.title,
            style: const TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 16,
            ),
          ),
          const SizedBox(height: 5),
          Text(
            reminderTime,
            style: const TextStyle(
              fontSize: 13,
              color: Colors.grey,
            ),
          ),
        ],
      ),
    );
  }
  
  Widget _buildCheckbox(bool isCompleted) {
    return GestureDetector(
      onTap: () => onToggle(!isCompleted),
      child: Container(
        width: 28,
        height: 28,
        decoration: BoxDecoration(
          color: isCompleted ? const Color(0xFF4CAF50) : Colors.transparent,
          border: Border.all(
            color: const Color(0xFF4CAF50),
            width: 2,
          ),
          borderRadius: BorderRadius.circular(14),
        ),
        child: isCompleted
            ? const Icon(
                Icons.check,
                color: Colors.white,
                size: 16,
              )
            : null,
      ),
    );
  }
}
